const co = require('co');
const pay = require('./pay');
const logger = require('../utils/logger');

// H5支付
const wapPayAddress = process.env.WAP_PAY_ADDRESS;
const wapPayMerchantID = process.env.WAP_PAY_MERCHANT_ID;
const wapPayKey = process.env.WAP_PAY_KEY;

function wapPaySign(params) {
    return pay.sign(params, wapPayKey);
}

function getWapPaySignedXml(params) {
    return pay.getSignedXml(params, wapPayKey);
}

let wapPay = co.wrap(function *(billno, amount, notifyUrl) {
    let params = {
        version: '1.0',
        spid: wapPayMerchantID,
        spbillno: billno,
        tranAmt: amount * 100,
        notifyUrl: notifyUrl,
        payType: 'pay.alipay.wap.native',
        backUrl: 'http://jmi.one/',
        productName: 'EOS Account Creation Service'
    };
    let xmlData = getWapPaySignedXml(params);
    logger.info(xmlData);

    return { retcode: 0, openUrl: `${wapPayAddress}?req_data=${encodeURIComponent(xmlData)}`, retmsg: 'success' };
});

module.exports = {
    wapPaySign,
    getWapPaySignedXml,
    wapPay,
    wapPayMerchantID
};